<?php

namespace Boer\Controller;

use Common\Controller\AdminbaseController;

class AboutusController extends AdminbaseController {

    protected $m_Article;
    protected $tid_list = array(
        '1'=>array(2,3,4,5),
        '2'=>array(13,14,15,16),
    );

    function _initialize() {
        parent::_initialize();
        $this->m_Article= D("Boer/Article");
    }

    /**
     * 列表页
     */
    public function index(){
        $where = array("a.language"=>session("language"),'a.tid'=>array("IN",$this->tid_list[session("language")]));
        $count=$this->m_Article->alias("a")->where($where)->count();
        $page = $this->page($count, 15);
        $list = $this->m_Article->alias("a")->field("a.*,t.title AS type_title")->join("cmf_article_type t ON t.id = a.tid","LEFT")
            ->where($where)
            ->order("update_time,create_time desc")
            ->limit($page->firstRow , $page->listRows)
            ->select();
        $this->assign("page", $page->show('Admin'));
        $this->assign("list",$list);
        $this->display();
    }


    /**
     * 新增
     */
    public function add(){
        $id = I("get.id",0,'intval');
        if(!empty($id)){
            $item=$this->m_Article->alias("a")->field("a.*,t.title AS type_title")->join("cmf_article_type t ON t.id = a.tid","LEFT")->where(array('a.id'=>$id))->find();
            $this->assign("item",$item);
        }

        $this->display();
    }

    /**
     * 新增数据提交
     */
    public function add_post(){
        if (IS_POST) {
            $id = I("post.id");
            $insert = array();
            $insert['content'] =htmlspecialchars_decode(I("post.content"));
            $insert['title'] =I("post.type_title");
            if(empty($id)){
                $insert['create_time'] = time();
                $insert['update_time'] = time();
                $insert['language'] = session("language");
                $result =$this->m_Article->add($insert);
            }else{
                $insert['update_time'] = time();
                $result = $this->m_Article->where(array("id"=>$id))->save($insert);
            }
            if ($result) {
                $this->success("操作成功！");
            } else {
                $this->error("操作失败！");
            }

        }
    }



    // 删除
    public function delete(){
        if(isset($_GET['id'])){
            $id = I("get.id",0,'intval');
            if ($this->m_Article->where(array('id'=>$id))->delete() !==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("参数有误！");
        }

    }

}